<?php

class AdminCategoriesController extends AppController {
    public $helpers = array('Html', 'Form');
	
	public $components = array(
        'RequestHandler',
        'Session',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'AdminCategorys', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'AdminCategorys', 'action' => 'display', 'home')
        )
    );
	var $uses = array(
		'Category'
	);
	
	public function beforeFilter() {
        parent::beforeFilter();
		$this->Auth->loginAction = array('plugin' => 'users', 'controller' => 'users', 'action' => 'login', 'admin' => false);
		$this->Auth->loginRedirect = '/';
		$this->Auth->logoutRedirect = '/';
		$this->Auth->authError = __('Sorry, but you need to login to access this location.', true);
		$this->Auth->loginError = __('Invalid e-mail / password
		combination.  Please try again', true);
		$this->Auth->autoRedirect = true;
		$this->Auth->userModel = 'User';
		$this->Auth->userScope = array('User.active' => 1, 'User.is_admin' => '1');
		if ($this->Auth->user()) {
			$this->set('userData', $this->Auth->user());
			$this->set('isAuthorized', ($this->Auth->user('id') != ''));
		}
		$this->layout = 'admin';
    }
	
	public function index() {
        $this->set('categories', $this->Category->find('all', array('conditions' => array('Category.id_parent' => null), 'order' => array('Category.id' => 'asc'))));
    }
	
	public function view($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid category'));
        }

        $category = $this->Category->findById($id);
        if (!$category) {
            throw new NotFoundException(__('Invalid category'));
        }
        $this->set('category', $category);
    }
	
	public function add() {
		if ($this->request->is(array('post', 'put'))) {
			$this->Category->create();
			if ($this->Category->save($this->request->data)) {
				$this->Session->setFlash(__('Your category has been saved.'), 'default', array(), 'success');
				return $this->redirect(array('action' => 'index'));
			}
			$this->Session->setFlash(__('Unable to add your category.'), 'default', array(), 'error');
		}
	}
	
	public function edit($id = null) {
		if (!$id) {
			throw new NotFoundException(__('Invalid category'));
		}
		$category = $this->Category->findById($id);
		
		if (!$category) {
			throw new NotFoundException(__('Invalid Category'));
		}
		
		if ($this->request->is(array('post', 'put'))) {
			$this->Category->id = $id;
			$this->Category->deleteAll(array('Category.id_parent' => $id));
			if ($this->Category->save($this->request->data)) {
				$this->Session->setFlash(__('Your Category has been updated.'));
				if($this->Category->saveAssociated($this->request->data))
					return $this->redirect(array('action' => 'index'));
			}
			$this->Session->setFlash(__('Unable to update your Category.'));
		}
		
		if (!$this->request->data) {		
			$this->set('sub_categories', $category['Children']);			
			$this->request->data = $category;
		}
	}
	
	public function delete($id) {
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}
		if ($this->Category->delete($id)) {
			$this->Session->setFlash(__('The Category with id: %s has been deleted.', h($id)));
			return $this->redirect(array('action' => 'index'));
		}
	}
	
	public function getSubCat() {
		$id_cat = $this->request->data('id_cat');
		 if (!$id_cat) {
            throw new NotFoundException(__('Invalid category'));
        }

        $category = $this->Category->findById($id_cat);
        if (!$category) {
            throw new NotFoundException(__('Invalid category'));
        }
		$subcats = $category['Children'];
        $this->set('subcats', $subcats);
		$this->set('_serialize', array('subcats'));
	}
	
}

?>